package com.csx.framework.springboot.sample.data.transaction;

import com.csx.framework.springboot.sample.data.transaction.entity.User;
import com.csx.framework.springboot.sample.data.transaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p> what does this class do ? </p>
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2024/12/21 周六 13:41
 */
@Component
public class TransactionalAnnotationSample {

    @Autowired
    private UserService userService;

    /**
     * 使用 Transactional 标记方法进行事务操作
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void testTransactionalAnnotation() {
        // 业务代码
        userService.insertUser(1001L, "王", "五", 18, "杭州");
        // 模拟插入失败,触发回滚
//        userService.insertUserFail(1002L, "王2", "五2", 18, "深圳");
        List<User> allUsers = userService.getAllUsers();
        System.out.println(allUsers);
    }

    public void test1() {
        System.out.println("test1");
    }

}
